package com.book.bridge.function;

import com.book.pojo.UserInfo;
import com.book.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

// 仅作为中间层 减少代码冗余
public abstract class AbstractRegisterLoginFun implements RegisterLoginFuncInterface{


    /**
     * 想要复用 login 、register、check，就必须把 UserService 拿过来，但是
     *
     * 抽象类中不能使用@Autowire 注入，即时抽象类也使用 @Component 标注也不能使用@Autowire注入
     *
     * 此时只能通过方法注入的方式，拿到UserService
     */

    protected String commonLogin(String account, String password, UserInfoService userInfoService){
        UserInfo userInfo = userInfoService.findByUserNameAndUserPassword(account,password);
        if (null == userInfo) {
            return "account / password Error";
        }
        return "Login Success";
    }

    public String commonRegister(UserInfo userInfo,UserInfoService userInfoService) {

        if(checkUserExists(userInfo.getUsername())){
            throw new RuntimeException("用户名已存在");
        }
        userInfo.setCreateDate(new Date());
        userInfoService.save(userInfo);
        return "Register Success";
    }

    public boolean commonCheckUserExists(String username,UserInfoService userInfoService) {
        return userInfoService.checkUserExists(username);
    }















    @Override
    public boolean checkUserExists(String username) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String login(String account, String password) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String login3rd(HttpServletRequest request) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String register(UserInfo userInfo) {
        throw new UnsupportedOperationException();
    }

}
